clear
set graphics off

local loopnum=${masteriter}

set seed [withheld]

local samplelist $samplelist
foreach sample in `samplelist'{

	if("`sample'"=="young${routine}" | "`sample'"=="youngold${routine}" | "`sample'"=="old${routine}"){
		local agegroup "youngold${routine}"
		}
	else{
		local agegroup "`sample'"
		}
			
		
local ltfpvar tfpvar

	quietly use "${data}/data_`agegroup'.dta", clear	
	if("`sample'"=="young${routine}"){
		keep if age<=5
	}
	if("`sample'"=="old${routine}"){
		keep if age>5
	}
		
	quietly cap rename `ltfpvar' lA

	*Groups	
	quietly egen JT		=group(J T)
	quietly egen ST		=group(S T)
	quietly egen SJT	=group(S J T)
	quietly egen tag_SJT	=tag(SJT)
	quietly egen tag_ST	=tag(ST)
	
	*Tau weights
	quietly bys SJT: egen emp_SJT	=total(empl)
	quietly bys ST: egen emp_ST	=total(empl)
	quietly bys JT: egen emp_JT	=total(empl)

	*Share of industry in total local employment
	quietly gen empshare_SJT		=emp_SJT/emp_ST
	
	tempfile originaldata_`ltfpvar'
	quietly save `originaldata_`ltfpvar''
	
*Starting point of loop
local i=1

while(`i'<=`loopnum'){

*di "Loop `i'"

	use `originaldata_`ltfpvar'', replace
	run "${code}/Covworker.do"
	foreach svar in capT tau {
			tempvar weight
		if("`svar'"=="capT"){
			local tag STR
			gen `weight'=1
			}
			else{
			local tag SJTR
			gen `weight'=empshare_SJT
			}
		quietly cor `svar'_Rb0 `svar'_Rb1  [aw=`weight'] if tag_`tag', cov
		quietly local cov_`svar'b=`r(cov_12)'
		quietly local var_`svar'=`r(Var_2)'
		}

		*Keep taus/capTs for the all sample.
		if("`sample'"=="all${routine}"){
		quietly keep if tag_STR | tag_SJTR
		quietly keep tau_Rb0 tau_Rb1 capT_Rb0 capT_Rb1 tag_STR tag_SJTR STR SJTR empshare_SJT
		quietly gen it=`i'
		if(`i'==1){
			quietly save "${results}/temp/temp_data_`ltfpvar'.dta", replace
			}
		else{
			quietly append using  "${results}/temp/temp_data_`ltfpvar'.dta"
			quietly save "${results}/temp/temp_data_`ltfpvar'.dta", replace
			}
		}
		quietly clear
		quietly set obs 1
		foreach svar in capT tau{
		quietly gen cov_`svar'b=`cov_`svar'b'
		quietly gen varhalf_`svar'=`var_`svar''
		quietly gen coef_`svar'=`cov_`svar'b'/varhalf_`svar'
		}
		quietly gen sample="`sample'"
		quietly gen iteration=`i'


if ("`i'"=="1" ){
	quietly save "${results}/temp/results_loop_`sample'_${date}.dta", replace
	}
	else{
	quietly append using "${results}/temp/results_loop_`sample'_${date}.dta"
	quietly save "${results}/temp/results_loop_`sample'_${date}.dta", replace
	}
 
local i=`i'+1
}


*Identify the optimal iteration
if ("`sample'"=="all${routine}"){
	local ltfpvar tfpvar
		foreach svar in capT tau{
			tempvar temp1 temp2 temp3
			*divide data into tfpvar of interest and the rest
			* we will only focus on tfvar of interest (=0 for sorting purposes)
			quietly egen `temp1'=mean(coef_`svar')
			quietly gen `temp2'	=abs(`temp1'-coef_`svar')
			sort `temp2'
			local optimalit_`svar'_`ltfpvar'=iteration[1]
					}
	
}

*Add 95cis
local ub	=round(.975*`loopnum')
local lb	=max(round(.025*`loopnum'),1)
foreach svar in capT tau{
quietly sort  cov_`svar'
 gen ci_ub_cov_`svar'=cov_`svar'[`ub']
 gen ci_lb_cov_`svar'=cov_`svar'[`lb']
}

foreach svar in capT tau{
	foreach var in coef cov varhalf{
		quietly gen mean_`var'_`svar' 	=`var'_`svar' 
		}
		}

quietly collapse (first) ci_* (mean) mean_* 
quietly keep *cov*
quietly gen sample="`sample'"
quietly save "${results}/temp/results_cov_`sample'_${date}${routine}.dta", replace

	 
*Run reg on optimal iteration
if("`sample'"=="all${routine}"){
local ltfpvar tfpvar
foreach svar in capT tau{
	use "${results}/temp/temp_data_`ltfpvar'.dta"  if it==`optimalit_`svar'_`ltfpvar'' 

	tempvar weight
 	if("`svar'"=="capT"){
			local tag STR
			gen `weight'=1
			}
			else{
			local tag SJTR
			gen `weight'=empshare_SJT
			}
			


	quietly binscatter `svar'_Rb0 `svar'_Rb1 [aw=`weight'] if tag_`tag', nq(25) savedata("${results}/temp/`svar'_`ltfpvar'") replace
		
		*Calculate covariances
		quietly  cor `svar'_Rb0 `svar'_Rb1  [aw=`weight'] if tag_`tag', cov
		quietly local cov_`svar'=`r(cov_12)'
		quietly local varhalf_`svar'=`r(Var_2)'
		
	quietly reg `svar'_Rb0 `svar'_Rb1  [aw=`weight'] if tag_`tag', r
	local `svar'_coef = _b[`svar'_Rb1]
	local `svar'_se = _se[`svar'_Rb1]
				
		
	clear
	set obs 1
	gen opt_varhalf_`svar'=`varhalf_`svar''
	gen opt_coef_coef_`svar'=``svar'_coef'
	gen opt_coef_se_`svar'=``svar'_se'
	gen sample = "`sample'"
	quietly merge 1:1  sample using "${results}/temp/results_cov_`sample'_${date}${routine}.dta", nogen
	quietly save "${results}/temp/results_cov_`sample'_${date}${routine}.dta", replace
	
	}
	
	quietly save "${results}/temp/results_cov_${date}${routine}.dta", replace
	}
	else{
	quietly append using "${results}/temp/results_cov_${date}${routine}.dta"
	quietly save "${results}/temp/results_cov_${date}${routine}.dta", replace
	}
}

quietly format ci* mean* opt* %9.4g
/*This file stores the numbers printed in footnotes 21 and 22 as well as the slopes 
and standard errors in Figures 3a and 3b.*/
quietly save "${results}/temp/results_cov_${date}${routine}.dta", replace
